/**
 * Created by Bian on 2021/3/31
 */
/* eslint-disable no-debugger, no-console */
import geoserverUtils from './geoserverUtils'
import coorTransformation from './coorTransformation'
export default {

	// 生成随机字符串
	generateRandomString(length) {
		let arr = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
		let result = ''
		for (var i = 0; i < length; i++) {
			let index = Math.round(Math.random() * (arr.length - 1))
			result += arr[index]
		}
		return result
	},

	// 生成随机数字
	generateRandomNumber(length) {
		let arr = '0123456789'
		let result = ''
		for (var i = 0; i < length; i++) {
			let index = Math.round(Math.random() * (arr.length - 1))
			result += arr[index]
		}
		return result
	},

	// 设置 localStorage
	setStorage: function (key, value, day) {
		localStorage[key] = JSON.stringify({
			value: value,
			expired: day === undefined ? undefined : Date.now() + 1000 * 60 * 60 * 24 * day
		})
	},

	// 获取 localStorage
	getStorage: function (key) {
		if (localStorage[key] === undefined) {
			return null
		}
		var obj = JSON.parse(localStorage[key])
		if (obj.expired === undefined || Date.now() < obj.expired) {
			return obj.value
		} else {
			delete localStorage[key]
		}
	},

	// 删除 localStorage
	delStorage: function (key) {
		localStorage.removeItem(key);
	},

	// 设置cookie
	setCookies: function (key, value) {
		document.cookie = key + "=" + escape(value)
	},

	// 获取cookie
	getCookies: function (name) {
		let arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
		if (arr = document.cookie.match(reg)) {
			return unescape(arr[2]);
		} else {
			return null;
		}
	},

	// 日期数据处理为 0000-00-00 格式
	dateFormat: function (date) {
		let year = date.getFullYear()
		let month = date.getMonth() + 1
		let day = date.getDate()
		month = month < 10 ? "0" + month : month
		day = day < 10 ? "0" + day : day
		return year + "-" + month + "-" + day
	},

	// 根据URL下载文件
	downloadFile(url, fileName) {
		var tempLink = document.createElement('a')
		tempLink.style.display = 'none'
		tempLink.href = url
		tempLink.setAttribute('download', fileName)

		if (typeof tempLink.download === 'undefined') {
			tempLink.setAttribute('target', '_blank')
		}

		document.body.appendChild(tempLink)
		tempLink.click()
		document.body.removeChild(tempLink)
	},

	// 判断对象非空状态
	isEmptyObj(obj) {
		return !obj || Object.keys(obj).length == 0
	},

	// 判断是否是url
	isValidURL(url) {
	  const regex = /(http|https):\/\/(\w+:{0,1}\w*)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%!\-\/]))?/
		return regex.test(url)
	},

	geoserverUtils,
	coorTransformation
}